package com.xdb.presistence.table.kv;

import java.text.MessageFormat;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.xdb.logging.LogFactory;
import com.xdb.logging.Logger;
import com.xdb.presistence.kv.TableRegister;
import com.xdb.presistence.metadata.ColumnMetadata;
import com.xdb.presistence.metadata.TableMetadata;
import com.xdb.presistence.table.XDBTableRow;
import com.xdb.presistence.table.XDBTableStore;

public class XDBTableStoreOnKV implements XDBTableStore {
	final static XDBTableStoreOnKV INSTANCE = new XDBTableStoreOnKV();
	
	public static XDBTableStoreOnKV getInstance(){
		return INSTANCE;
	}
	
	private XDBTableStoreOnKV(){}
	
	final TableRegister tableRegister = new TableRegister();
	static Logger LOGGER = LogFactory.getLog(XDBTableStoreOnKV.class);
	@Override
	public void create(TableMetadata metadata) {
		LOGGER.debug("开始创建表");
		tableRegister.bind(metadata, new HashMap<String, String>());
		LOGGER.debug("结束创建表");
	}

	@Override
	public int drop(String tabName) {
		int size = truncate(tabName);
		tableRegister.unbind(tabName);
		return size;
	}

	@Override
	public int truncate(String tabName) {
		Map<String, String> table = tableRegister.lookup(tabName);
		int size = table.size();
		table.clear();
		return size;
	}

	@Override
	public int insert(String tabName, XDBTableRow... rows) {
		//从注册中心获取表对象
		Map<String, String> table = tableRegister.lookup(tabName);
		//获取表元信息
		TableMetadata metadata = tableRegister.lookupMetadata(tabName);
		//获取列
		List<String> cols = metadata.getColumns();
		//开始组装K和V对应的JSON模板
		StringBuilder recordBuffer = new StringBuilder();
		int index = 0;
		for (String name : cols) {
			recordBuffer.append("\"").append(name).append("\":").append("\"").append("{"+index+"}").append("\"");
			index++;
			if(index < cols.size()){
				recordBuffer.append(",");
			}
		}
		StringBuilder pkBuffer = new StringBuilder();
		String[] pks = metadata.getPks();
		index = 0;
		for (String pk : pks) {
			pkBuffer.append("\"").append(pk).append("\":").append("\"").append("{"+index+"}").append("\"");
			index++;
			if(index < pks.length){
				pkBuffer.append(",");
			}
		}
		String templet = recordBuffer.toString();
		String pktemplet = pkBuffer.toString();
		LOGGER.debug("templet:" + templet);
		LOGGER.debug("pktemplet:" + pktemplet);
		//将数据应用到模板上
		int count = 0;
		for (XDBTableRow row : rows) {
			System.out.println(Arrays.toString(row.getValues()));
			String k = MessageFormat.format(pktemplet, row.getValues());
			String v = MessageFormat.format(templet, row.getValues());
			LOGGER.debug("k:" + k);
			LOGGER.debug("v:" + v);
			table.put("{" + k + "}", "{" + v + "}");
			count++;
		}
		return count;
	}

	@Override
	public int delete(String tabName, Map<String, String> keywords) {
		// TODO 自动生成的方法存根
		return 0;
	}

	@Override
	public int update(String tabName, XDBTableRow... newRows) {
		// TODO 自动生成的方法存根
		return 0;
	}

	@Override
	public List<XDBTableRow> select(String tabName, Map<String, String> keywords) {
		// TODO 自动生成的方法存根
		return null;
	}

}
